<template>
	<el-main class="bg edit_wrap comtable_e">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">
		<el-row class="row_ce"> 
							<el-col v-if="$check_field('get','exchange_title') || $check_field('add','exchange_title') || $check_field('set','exchange_title')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="交流标题" prop="exchange_title">
															<el-input id="exchange_title" v-model="form['exchange_title']" placeholder="请输入交流标题"
							  v-if="(form['community_communication_id'] && $check_field('set','exchange_title')) || (!form['community_communication_id'] && $check_field('add','exchange_title'))" :disabled="disabledObj['exchange_title_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','exchange_title')">{{form['exchange_title']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','communication_classification') || $check_field('add','communication_classification') || $check_field('set','communication_classification')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="交流分类" prop="communication_classification">
											<el-select id="communication_classification" v-model="form['communication_classification']"						v-if="(form['community_communication_id'] && $check_field('set','communication_classification')) || (!form['community_communication_id'] && $check_field('add','communication_classification'))">
						<el-option v-for="o in list_communication_classification" :key="o['communication_classification']" :label="o['communication_classification']"
							:value="o['communication_classification']">
						</el-option>
					</el-select>
					<div v-else-if="$check_field('get','communication_classification')">{{form['communication_classification']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','communication_tags') || $check_field('add','communication_tags') || $check_field('set','communication_tags')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="交流标签" prop="communication_tags">
															<el-input id="communication_tags" v-model="form['communication_tags']" placeholder="请输入交流标签"
							  v-if="(form['community_communication_id'] && $check_field('set','communication_tags')) || (!form['community_communication_id'] && $check_field('add','communication_tags'))" :disabled="disabledObj['communication_tags_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','communication_tags')">{{form['communication_tags']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','student_users') || $check_field('add','student_users') || $check_field('set','student_users')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="学生用户" prop="student_users">
																					<div v-if="user_group !== '管理员'">
							{{ get_user_session_student_users(form['student_users']) }}
							<el-select v-if="(form['community_communication_id'] && $check_field('set','student_users')) || (!form['community_communication_id'] && $check_field('add','student_users'))" id="student_users" v-model="form['student_users']" :disabled="disabledObj['student_users_isDisabled']">
								<el-option v-for="o in list_user_student_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
							<el-select v-else-if="$check_field('get','student_users')" id="student_users" v-model="form['student_users']" :disabled="true">
								<el-option v-for="o in list_user_student_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
										   :value="o['user_id']">
								</el-option>
							</el-select>
						</div>
						<el-select v-else id="student_users" v-model="form['student_users']" :disabled="disabledObj['student_users_isDisabled']">
							<el-option v-for="o in list_user_student_users" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
																</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','communication_description') || $check_field('add','communication_description') || $check_field('set','communication_description')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="交流描述" prop="communication_description">
											<el-input type="textarea" id="communication_description" v-model="form['communication_description']" placeholder="请输入交流描述"
						v-if="(form['community_communication_id'] && $check_field('set','communication_description')) || (!form['community_communication_id'] && $check_field('add','communication_description'))" :disabled="disabledObj['communication_description_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','communication_description')">{{form['communication_description']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','cover_photo') || $check_field('add','cover_photo') || $check_field('set','cover_photo')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
							<el-form-item label="封面图片" prop="cover_photo">
											<el-upload :disabled="disabledObj['cover_photo_isDisabled']" class="avatar-uploader" drag
						accept="image/gif, image/jpeg, image/png, image/jpg" action="" :http-request="upload_cover_photo"
						:show-file-list="false" v-if="(form['community_communication_id'] && $check_field('set','cover_photo')) || (!form['community_communication_id'] && $check_field('add','cover_photo'))">
						<img id="cover_photo" v-if="form['cover_photo']" :src="$fullUrl(form['cover_photo'])" class="avatar">
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
					<el-image v-else-if="$check_field('get','cover_photo')" style="width: 100px; height: 100px"
						:src="$fullUrl(form['cover_photo'])" :preview-src-list="[$fullUrl(form['cover_photo'])]">
						<div slot="error" class="image-slot">
							<img src="../../../public/img/error.png" style="width: 90px; height: 90px" />
						</div>
					</el-image>
							</el-form-item>
			</el-col>
								<el-col v-if="$check_field('get','communication_content') || $check_field('add','communication_content') || $check_field('set','communication_content')" :xs="24" :sm="24" :lg="24" class="el_form_editor_warp">
							<el-form-item label="交流内容" prop="communication_content">
								<quill-editor v-model.number="form['communication_content']"
						v-if="(form['community_communication_id'] && $check_field('set','communication_content')) || (!form['community_communication_id'] && $check_field('add','communication_content')) ">
					</quill-editor>
					<div v-else-if="$check_field('get','communication_content')" v-html="form['communication_content']"></div>
				</el-form-item>
			</el-col>
								<el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="审核状态" prop="examine_state">
					<el-select id="examine_state" v-model="form['examine_state']"
						v-if="(form['examine_state'] && $check_examine()) || (!form['examine_state'] && $check_examine())" :disabled="true">
						<el-option key="未审核" label="未审核" value="未审核"></el-option>
						<el-option key="已通过" label="已通过" value="已通过"></el-option>
						<el-option key="未通过" label="未通过" value="未通过"></el-option>
					</el-select>
					<div v-else>{{form["examine_state"]}}</div>
				</el-form-item>
			</el-col>
					<el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="审核回复" prop="examine_reply">
					<el-input id="examine_reply" v-model="form['examine_reply']" placeholder="请输入审核回复"
						v-if="(form['examine_reply'] && $check_examine()) || (!form['examine_reply'] && $check_examine())" :disabled="true"></el-input>
					<div v-else>{{form["examine_reply"]}}</div>
				</el-form-item>
			</el-col>
	
	
	
		
		
						<el-col :xs="24" :sm="12" :lg="8" class="el_form_item_warp" v-if="!form['source_user_id'] && 0 > 0">
			  <el-form-item label="举报限次">
				<el-input id="limit_times" v-model="form['report_information_limit_times']" placeholder="举报限制次数，0为不限"
						  v-if="$check_option('/community_communication/view','can_limits')"
				></el-input>
				<div v-else-if="$check_action('/community_communication/view','get')" v-html="form['report_information_limit_times']"></div>
			  </el-form-item>
			</el-col>
			
	
	
		
		
	
		</el-row>
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
				<el-form-item v-if="$check_action('/community_communication/view','set') || $check_action('/community_communication/view','add') || $check_option('/community_communication/table','examine')">
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
				</el-form-item>
				<el-form-item v-else>
					<el-button @click="cancel()">返回</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";
	export default {
		mixins: [mixin],
		data() {
			return {
				field: "community_communication_id",
				url_add: "~/api/community_communication/add?",
				url_set: "~/api/community_communication/set?",
				url_get_obj: "~/api/community_communication/get_obj?",
				url_upload: "~/api/community_communication/upload?",

				query: {
					"community_communication_id": 0,
				},

				form: {
								"exchange_title":  '', // 交流标题
										"communication_classification":  '', // 交流分类
										"communication_tags":  '', // 交流标签
										"student_users": 0, // 学生用户
										"communication_description":  '', // 交流描述
										"cover_photo":  '', // 封面图片
										"communication_content":  '', // 交流内容
									"examine_state": "未审核",
							"examine_reply": "",
							"community_communication_id": 0, // ID
													"report_information_limit_times": 0, // 举报限制次数
												},
				disabledObj:{
								"exchange_title_isDisabled": false,
										"communication_classification_isDisabled": false,
										"communication_tags_isDisabled": false,
										"student_users_isDisabled": false,
										"communication_description_isDisabled": false,
										"cover_photo_isDisabled": false,
										"communication_content_isDisabled": false,
										},

	
									// 交流分类选项列表
				list_communication_classification: [""],
	
			
			
					// 用户列表
				list_user_student_users: [],
						// 用户组
				group_user_student_users: "",
					
			
			
		
			}
		},
		methods: {

	
	
			
				/**
			 * 获取交流分类列表
			 */
			
						async get_list_communication_classification() {
				var json = await this.$get("~/api/communication_classification/get_list?");
				if(json.result && json.result.list){
					this.list_communication_classification = json.result.list;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
								
			
	
			
	
				/**
			 * 获取学生用户用户列表
			 */
			async get_list_user_student_users() {
                var json = await this.$get("~/api/user/get_list?user_group=学生用户");
                if(json.result && json.result.list){
                    this.list_user_student_users = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
					/**
			 * 获取学生用户用户组
			 */
			async get_group_user_student_users() {
							this.form["student_users"] = this.user.user_id;
							var json = await this.$get("~/api/user_group/get_obj?name=学生用户");
				if(json.result && json.result.obj){
					this.group_user_student_users = json.result.obj;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			get_user_session_student_users(id){
				var _this = this;
				var user_id = {"user_id":id}
				var url = "~/api/"+_this.group_user_student_users.source_table+"/get_obj?"
				this.$get(url, user_id, function(res) {
					if (res.result && res.result.obj) {
						var arr = []
						for (let key in res.result.obj) {
							arr.push(key)
						}
						var arrForm = []
									for (let key in _this.form) {
							arrForm.push(key)
						}
												_this.form["student_users"] = id
									_this.disabledObj['student_users' + '_isDisabled'] = true
						for (var i=0;i<arr.length;i++){
						  if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
							for (var j = 0; j < arrForm.length; j++) {
							  if (arr[i] === arrForm[j]) {
								if (arr[i] !== "student_users") {
			                      _this.form[arrForm[j]] = res.result.obj[arr[i]]
			                      _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								  break;
								} else {
								  _this.disabledObj[arrForm[j] + '_isDisabled'] = true
								}
							  }
							}
						  }
						}
					}
				});
			},
					get_user_student_users(id){
				var obj = this.list_user_student_users.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},
			
	
						/**
			 * 上传封面图片
			 * @param {Object} param 图片参数
			 */
			upload_cover_photo(param){
									this.uploadFile(param.file, "cover_photo");
								},
	
	
			
	
		
			/**
			 * 获取对象之前
			 * @param {Object} param
			 */
			get_obj_before(param) {
				var form = "";
												
				if(this.form && form){
					Object.keys(this.form).forEach(key => {
						Object.keys(form).forEach(dbKey => {
							// if(dbKey === "charging_standard"){
							// 	this.form['charging_rules'] = form[dbKey];
							// 	this.disabledObj['charging_rules_isDisabled'] = true;
							// };
							if(key === dbKey){
								this.disabledObj[key+'_isDisabled'] = true;
								this.form[key] = form[dbKey]
							}
							if(dbKey === "source_table"){
								this.form['source_table'] = form[dbKey];
							}
							if(dbKey === "source_id"){
								this.form['source_id'] = form[dbKey];
							}
							if(dbKey === "source_user_id"){
								this.form['source_user_id'] = form[dbKey];
							}
						})
					})
				}
																		$.db.del("form");

				return param;
			},

			/**
			 * 获取对象之后
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json, func){
																																			

			},

																																																													async submit(param, func){
				if (!param) {
					param = this.form;
				}
						
				var pm = this.events("submit_before", Object.assign({}, param)) || param;
				var msg = await this.events("submit_check", pm);
				var ret;
				if (msg) {
					this.$toast(msg, 'danger');
				} else {
																																																									ret = this.events("submit_main", pm, func);
				}
				return ret;
			},
			
			/**
			 * 提交前验证事件
			 * @param {Object} 请求参数
			 * @return {String} 验证成功返回null, 失败返回错误提示
			 */
						submit_check(param) {
					
																																																																																			return null;
			},

			is_view(){
				// var bl = this.user_group == "管理员";
				var bl = false;

				if(!bl){
					bl = this.$check_action('/community_communication/table','add');
					console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
				}
				if(!bl){
					bl = this.$check_action('/community_communication/table','set');
					console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
				}
				if(!bl){
					bl = this.$check_action('/community_communication/view','add');
					console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
				}
				if(!bl){
					bl = this.$check_action('/community_communication/view','set');
					console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
				}
				if(!bl){
					bl = this.$check_action('/community_communication/view','get');
					console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
				}

				console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

				return bl;
			},
			/**
			 * 上传文件
			 * @param {Object} param
			 */
			uploadimg(param) {
				this.uploadFile(param.file, "avatar");
			},

		},
		created() {
						this.get_list_communication_classification();
									this.get_list_user_student_users();
					this.get_group_user_student_users();
											},
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
	
	.img_multiple{
		overflow: hidden;
	}
	.img_multiple .img_block{
		float: left;
		margin-right: 5px;
		margin-bottom: 5px;
		position: relative;
	}
	.img_multiple .img_block img{
		height: 100px;
		width: auto;
	}
	.img_multiple .img_del{
		position: absolute;
		top: 5px;
		right: 5px;
		width: 20px;
		height: 20px;
		background: #0000008a;
		color: #fff;
		line-height: 20px;
		text-align: center;
		border-radius: 100%;
		cursor: pointer;
	}




	
</style>
